# -*- coding: utf-8 -*-

import os, re, time
import demjson
import html as ht
from lxml import etree
from lxml.etree import XPath
from urllib.parse import quote
from urllib.parse import unquote_plus
from spider.gonggao.GonggaoBase import GonggaoBase

from mysql.MysqlClient import MysqlClient
from txt.TXTClient import TXTClient

'''
公告-福星股份
'''
class Gonggao(GonggaoBase):

    def __init__(self, fmt):
        #super(Gonggao, self).__init__(fmt)  # 在python2中，需要super(自己的类名,self)来调用父类中的属性
        GonggaoBase.__init__(self, fmt)#用GonggaoBase调用__init__的方式重用父类功能

    #详细页处理
    def detail_page(self, url, file_path) :
        htmlText = self.requestHtml('GET', url, self.headers)
        #print(htmlText)
        if htmlText==None or htmlText.find('运行时错误') > 0:
            return None
        html = etree.HTML(htmlText)

        try:
            content_xpath = XPath(self.content_xpath)
            content = self.host_url.strip() + content_xpath(html)[0]

        except Exception as e:
            print(e)
        finally:
            return content

if __name__ == '__main__':
    #记录执行开始时间
    str_time = int(time.time())
    txt_client = TXTClient()
    # 获取数据库连接
    sql_client = MysqlClient()
    connect, cursor = sql_client.conn_istorage()

    #实例化类
    obj = Gonggao('%Y%m%d')

    cols = "id, company_name, index_url, content_url, host_url, html_file_path, `method`, headers, cookie, params, `data`, json, outer_xpath, inner_xpath, other_xpath, date_xpath, filename_xpath, href_xpath, content_xpath, start_page, end_page"
    query_sql = "select " + cols + " from company_spider where status = '0' and id = %d" % (19)
    #indexDict = obj.makeColIndex(cols)

    queryResult = sql_client.query_data(connect, cursor, query_sql)
    print(type(queryResult))
    convertList = obj.main(queryResult)

    connect.close()

    end_time = int(time.time())
    print("程序执行时间（秒）：", end_time - str_time)
